dojo.require("market.widgets.Cache");
dojo.require("market.widgets.Ajax");
dojo.require("market.widgets.GenericForm");
dojo.require("market.widgets.component.Participant");
dojo.require("market.widgets.GenericMessage");
var SYSTEMWIDGET = {
_opmCache:new market.widgets.Cache({maxItems:50}),
cacheWidget:function(){ return this._opmCache; },
callAjaxWidget:function(data,ajaxType){
   switch (ajaxType){
   case "callXhrGetJson":
      SYSTEMWIDGET.ajaxWidget(data).callXhrGetJson();
      break;
   }
},
ajaxWidget:function(data){
   var ajaxWidget=this._commonWidget(data,"ajaxWidget",function(args){
      return new market.widgets.Ajax(args);
   });
   ajaxWidget.clear(data);
   if (data == null || data.hasCache == null) ajaxWidget.hasCache = false;
   return ajaxWidget;
},
messageWidget:function(){
   var customWidget = this._commonWidget(null,"messageWidget",function(args){
      return new market.widgets.GenericMessage(args);
   });
   return customWidget;
},
content:function(data){
   dojo.require("market.widgets.component.Content");
   var customWidget = this._commonWidget(data,"contentWidget",function(args){
      return new market.widgets.component.Content(args);
   });
   return customWidget;
},
comment:function(data){
   dojo.require("market.widgets.component.Comment");
   var customWidget = this._commonWidget(data,"comment",function(args){
      return new market.widgets.component.Comment(args);
   });
   return customWidget;
},
tribe:function(data){
   dojo.require("market.widgets.component.Tribe");
   var customWidget = this._commonWidget(data,"tribe",function(args){
      return new market.widgets.component.Tribe(args);
   });
   return customWidget;
},
configuration:function(data){
   dojo.require("market.widgets.component.Configuration");
   var customWidget = this._commonWidget(data,"configuration",function(args){
      return new market.widgets.component.Configuration(data);
   });
   return customWidget;
},
relationship:function(data){
   dojo.require("market.widgets.component.Relationship");
   var customWidget = this._commonWidget(data,"relationship",function(args){
      return new market.widgets.component.Relationship(data);
   });
   return customWidget;
},
tribeLayout:function(data){
   dojo.require("market.widgets.component.TribeLayout");
   var customWidget = this._commonWidget(data,"tribeLayout",function(args){
      return new market.widgets.component.TribeLayout(args);
   });
   return customWidget;
},
tribeNews:function(data){
   dojo.require("market.widgets.component.TribeNews");
   var customWidget = this._commonWidget(data,"tribeNews",function(args){
      return new market.widgets.component.TribeNews(args);
   });
   return customWidget;
},
contentEditor:function(data,refresh){
   dojo.require("market.widgets.component.ContentEditor");
   var customWidget = null;
   if (refresh==true){
      customWidget=new market.widgets.component.ContentEditor(data);
   }else{
      customWidget=this._commonWidget(data,"contentEditorWidget",function(args){
         return new market.widgets.component.ContentEditor(args);
      });
   }
   return customWidget;
},
pictures : function(data){
   dojo.require("market.widgets.component.Pictures");
   var customWidget = this._commonWidget(data, "picturesWidget", function(args){
      return new market.widgets.component.Pictures(args);
   });
   return customWidget;
},
showPictures : function(data){
   dojo.require("market.widgets.component.ShowPictures");
   var customWidget = this._commonWidget(data, "showPicturesWidget", function(args){
      return new market.widgets.component.ShowPictures(args);
   });
   return customWidget;
},
album:function(data){
   dojo.require("market.widgets.component.Album");
   var customWidget = this._commonWidget(data, "albumWidget", function(args){
      return new market.widgets.component.Album(args);
   });
   return customWidget;
},
participant:function(data){
   var customWidget = this._commonWidget(data, "participantWidget", function(args){
      return new market.widgets.component.Participant(args);
   });
   return customWidget;
},
genericFormWidget: function(data){
   var customWidget = this._commonWidget(data, data.widgetId, function(args){
      return new market.widgets.GenericForm(args);
   });
   return customWidget;
},
_commonWidget:function(data,widgetId,instanceFunction){
   var _args = data;
   if (_args==null || _args.hasReset==true)
      _args = {};
   _args.id = widgetId;
   _args.widgetId= widgetId;
   return this._getWidget(_args, instanceFunction);
},
_getWidget:function (args, newInstanceFunction){
   var commonWidget = dijit.byId(args.widgetId);
   if (commonWidget == null){
      commonWidget = newInstanceFunction(args);
   }
   for (var property in args){
      switch (property){
      case "dataform":
         commonWidget.setDataForm(args[property]);
         break;
      case "dataPage":
         commonWidget.setDataPage(args[property]);
         break;
      default:
         commonWidget[property] = args[property];
         break;
      }
   }
   return commonWidget;
}};
function _initMain (){
   dojo.query('#jregister').onclick(function(){
      SYSTEMWIDGET.participant({contentId:'divRegisterForm'}).showRegisterPage();
   });
}
function _initMarket (){
   SYSTEMWIDGET.participant().init();
}
function getContentGUID(contentGUID){
   if (contentGUID)
      return contentGUID;
   return -1;
}
function getPartGUID(partcp){
   return getContentGUID(partcp.avatar.contentFile.guid);
}
function getPartViewName(partcp){
   return getViewName(partcp.firstName,partcp.lastName,partcp.nickname);
}
function getViewName(firstName,lastName,nickName){
   if (nickName==null || nickName.length==0){
      return firstName+" "+lastName;
   }
   return nickName;
}
function getCurrentName(){
   return getViewName(participant.firstName,participant.lastName,participant.nickname);
}
